package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.repository.statistics.RegisterNumStatRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.statistics.RegisterNumStatRequest;
import com.tuanzi.loan.vo.response.statistics.RegisterNumStatResponse;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.SelectField;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.tuanzi.loan.generated.jooq.stat.tables.StatRegisterNumByHour.STAT_REGISTER_NUM_BY_HOUR;
@Repository
public class RegisterNumStatRepositoryImpl extends JOOQBaseRepositoryPlus implements RegisterNumStatRepository {


    @Override
    public Page<RegisterNumStatResponse> list(RegisterNumStatRequest searchRequest, Pageable pageable) {
        List<SelectField<?>> selectFields = new ArrayList<>();
        selectFields.add(STAT_REGISTER_NUM_BY_HOUR.DATE_H);
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.ZERO_H,Integer.class)).as("zero_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.ONE_H,Integer.class)).as("one_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.TWO_H,Integer.class)).as("two_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.THREE_H,Integer.class)).as("three_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.FOUR_H,Integer.class)).as("four_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.FIVE_H,Integer.class)).as("five_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.SIX_H,Integer.class)).as("six_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.SEVEN_H,Integer.class)).as("seven_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.EIGHT_H,Integer.class)).as("eight_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.NINE_H,Integer.class)).as("nine_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.TEN_H,Integer.class)).as("ten_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.ELEVEN_H,Integer.class)).as("eleven_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.TWELVE_H,Integer.class)).as("twelve_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.THIRTEEN_H,Integer.class)).as("thirteen_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.FOURTEEN_H,Integer.class)).as("fourteen_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.FIFTEEN_H,Integer.class)).as("fifteen_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.SIXTEEN_H,Integer.class)).as("sixteen_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.SEVENTEEN_H,Integer.class)).as("seventeen_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.EIGHTEEN_H,Integer.class)).as("eighteen_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.NINETEEN_H,Integer.class)).as("nineteen_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.TWENTY_H,Integer.class)).as("twenty_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.TWENTY_ONE_H,Integer.class)).as("twenty_one_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.TWENTY_TWO_H,Integer.class)).as("twenty_two_h"));
        selectFields.add(DSL.sum(DSL.cast(STAT_REGISTER_NUM_BY_HOUR.TWENTY_THREE_H,Integer.class)).as("twenty_three_h"));


        //如果选择的是全部，则进行求和操作
        if (!(searchRequest.getChannel() != null && searchRequest.getChannel().contains("9999"))){
            selectFields.add(STAT_REGISTER_NUM_BY_HOUR.CHANNEL);
            in(STAT_REGISTER_NUM_BY_HOUR.CHANNEL,searchRequest.getChannel());
            groupBy(STAT_REGISTER_NUM_BY_HOUR.CHANNEL);
        }
        SelectField[] selectArray = new SelectField[selectFields.size()];
        selectFields.toArray(selectArray);
        fields(selectArray);
        if (Objects.nonNull(searchRequest.getStartDate())){
            ge(STAT_REGISTER_NUM_BY_HOUR.DATE_H,searchRequest.getStartDate());
        }
        if (Objects.nonNull(searchRequest.getEndDate())){
            le(STAT_REGISTER_NUM_BY_HOUR.DATE_H,searchRequest.getEndDate());
        }
        groupBy(STAT_REGISTER_NUM_BY_HOUR.DATE_H);
        return search(pageable, new RecordMapper<Record, RegisterNumStatResponse>() {
            @Override
            public RegisterNumStatResponse map(Record record) {
                RegisterNumStatResponse response = new RegisterNumStatResponse();
                response.setDate_h(record.get(STAT_REGISTER_NUM_BY_HOUR.DATE_H));
                if (!(searchRequest.getChannel() != null && searchRequest.getChannel().contains("9999"))) {
                    if ("-2000".equals(record.get(STAT_REGISTER_NUM_BY_HOUR.CHANNEL))){
                        response.setChannel("空渠道");
                    }else{
                        response.setChannel(record.get(STAT_REGISTER_NUM_BY_HOUR.CHANNEL));
                    }
                }else{
                    response.setChannel("全部");
                }
                response.setZero_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("zero_h"),String.class));
                response.setOne_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("one_h"),String.class));
                response.setTwo_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("two_h"),String.class));
                response.setThree_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("three_h"),String.class));
                response.setFour_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("four_h"),String.class));
                response.setFive_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("five_h"),String.class));
                response.setSix_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("six_h"),String.class));
                response.setSeven_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("seven_h"),String.class));
                response.setEight_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("eight_h"),String.class));
                response.setNine_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("nine_h"),String.class));
                response.setTen_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("ten_h"),String.class));

                response.setEleven_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("eleven_h"),String.class));
                response.setTwelve_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("twelve_h"),String.class));
                response.setThirteen_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("thirteen_h"),String.class));
                response.setFourteen_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("fourteen_h"),String.class));
                response.setFifteen_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("fifteen_h"),String.class));
                response.setSixteen_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("sixteen_h"),String.class));
                response.setSeventeen_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("seventeen_h"),String.class));
                response.setEighteen_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("eighteen_h"),String.class));
                response.setNineteen_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("nineteen_h"),String.class));
                response.setTwenty_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("twenty_h"),String.class));

                response.setTwenty_one_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("twenty_one_h"),String.class));
                response.setTwenty_two_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("twenty_two_h"),String.class));
                response.setTwenty_three_h(record.get(STAT_REGISTER_NUM_BY_HOUR.field("twenty_three_h"),String.class));

                return response;
            }
        });
    }

    @Override
    protected TableImpl getTable() {
        return STAT_REGISTER_NUM_BY_HOUR;
    }
}
